package com.controller.agent;

import com.biz.BdUserinfoBiz;
import com.biz.BillBiz;
import com.core.Result;
import com.core.ResultGenerator;
import com.entity.model.BdUserinfo;
import com.entity.model.Bill;
import com.github.pagehelper.PageInfo;
import com.util.SystemConstant;
import com.util.webpage.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Condition;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by jc on 2020/01/06.
 */
@Controller
@RequestMapping("/agent/security/bdUserInfo")
public class AgentBdUserinfoController extends BaseController<BdUserinfo> {
    @Autowired
    private BdUserinfoBiz bdUserinfoBiz;

    @Autowired
    private BillBiz billBiz;

    @RequestMapping(value = "index")
    public String index() throws Exception {
        return "/agent/security/bdUserInfo/list";
    }

    @RequestMapping(value = "detail")
    public String detail() throws Exception {
        return "/agent/security/bdUserInfo/form";
    }

    @RequestMapping(value = "bdUserStatistics")
    public String bdUserStatistics() throws Exception {
        return "/agent/security/bdUserStatistics/list";
    }

    @RequestMapping(value = "billList")
    public String billList() throws Exception {
        return "/agent/security/bdUserStatistics/billList";
    }

    //获取代理商员工推广订单列表详情
    @RequestMapping(value = "commissionUserBillDetailList")
    @ResponseBody
    public Result commissionUserBillDetailList(Integer parentBdUserId) {
        PageData cond = new PageData();
        cond.put("parentBdUserId", parentBdUserId);
        List<Bill> list = billBiz.commissionUserBillList(cond);
        PageInfo<Bill> pageInfo = new PageInfo<Bill>(list);
        Integer total = (int) pageInfo.getTotal();
        return ResultGenerator.genSuccessResult(list, total);
    }

    @RequestMapping(value = "bdUserInfoList")
    @ResponseBody
    public Result bdUserInfoList(HttpSession session) {
        BdUserinfo managerInfo = (BdUserinfo) session.getAttribute(SystemConstant.SESSION_AGENT_ADMIN);
        Condition condition = new Condition(BdUserinfo.class);
        List<BdUserinfo> list = null;
        if (managerInfo.getIsSuper() == 1) {
            condition.createCriteria().andEqualTo("bdTeamId", managerInfo.getBdTeamId()).andEqualTo("isSuper", 0);
            list = bdUserinfoBiz.findByCondition(condition);
        } else {
            condition.createCriteria().andEqualTo("bdTeamId", managerInfo.getBdTeamId()).andEqualTo("id", managerInfo.getId());
            list = bdUserinfoBiz.findByCondition(condition);
        }
        return ResultGenerator.genSuccessResult(list);
    }

    @RequestMapping(value = "addTeam")
    @ResponseBody
    public Result addTeam(HttpSession session, BdUserinfo bdUserinfo) {
        BdUserinfo managerInfo = (BdUserinfo) session.getAttribute(SystemConstant.SESSION_AGENT_ADMIN);
        if (managerInfo.getIsSuper() == 1) {
            Condition condition = new Condition(BdUserinfo.class);
            condition.createCriteria().andEqualTo("account", bdUserinfo.getAccount());
            if (bdUserinfoBiz.findByCondition(condition).size() > 0) {
                return ResultGenerator.genFailResult("该账号已存在,请更换");
            } else {
                bdUserinfo.setBdTeamId(managerInfo.getBdTeamId());
                bdUserinfoBiz.save(bdUserinfo);
                return ResultGenerator.genSuccessResult();
            }
        } else {
            return ResultGenerator.genFailResult("您不是管理员,无法新增人员");
        }
    }

    @RequestMapping(value = "bdUserStatisticsList")
    @ResponseBody
    public Result bdStatisticsList(HttpSession session, HttpServletRequest request) {
        BdUserinfo managerInfo = (BdUserinfo) session.getAttribute(SystemConstant.SESSION_AGENT_ADMIN);

        PageData cond = new PageData(request);
        cond.put("bdTeamId", managerInfo.getBdTeamId());
        List<Bill> list = billBiz.bdUserStatistics(cond);
        PageInfo<Bill> pageInfo = new PageInfo<Bill>(list);
        Integer total = (int) pageInfo.getTotal();
        return ResultGenerator.genSuccessResult(list, total);
    }
}
